import {getToken,removeToken} from './auth.js'

export const config = {
	baseUrl: 'http://116.62.237.64:8080/',
}

const request = async function(url,parms,method){
	try{
		const token = getToken()
		if(!token){
			const pages = getCurrentPages();
			console.log(pages.route)
			// console.log(pages[pages.length -1].route)
			// let page;
			// if(pages.length > 0 ){
			// 	uni.clearStorageSync('page')
			// 	page = pages[pages.length -1].route;
			// }
			// page !== '/pages/login/login' && uni.setStorageSync('page',page)
			console.log(pages[pages.length - 1])
			// if(pages.route !== '/pages/login/login'){
			// 	uni.showToast({
			// 		title:"登录已过期"
			// 	})
				uni.navigateTo({
					url: '/pages/login/login' 
				})
		}
		const result = await uni.request({
			url:config.baseUrl+ url,
			method,
			data: parms || {},
			header:{
				Authorization:token
			}
		})
		if(result[1].statusCode !== 200){
			uni.showToast({
				title: result[1].data.message
			})
		}
		if(result[1].data.message == '账号密码错误'){
			uni.showToast({
				title:result[1].data.message
			})
		}	
		return result[1].data.data;
	}catch(e){
		console.log(e)
		uni.showToast({
			title:'登录过期'
		})
	}
}

export default request;